package cn.droidlover.xdroidmvp.net;

import android.content.Context;
import android.util.Log;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import javax.net.ssl.SSLHandshakeException;

import cn.droidlover.xdroidmvp.log.XLog;
import io.reactivex.functions.Consumer;
import io.reactivex.subscribers.ResourceSubscriber;
import okhttp3.internal.http2.ConnectionShutdownException;
import retrofit2.HttpException;


/**
 * Created by wanglei on 2016/12/26.
 */

public abstract class ApiSubscriber<T> extends ResourceSubscriber<T> {

    private Context mContext;
    private Consumer<ApiException> errorAction;

    public ApiSubscriber(Consumer<ApiException> errorAction) {
        this.errorAction = errorAction;
    }

    public ApiSubscriber() {

    }

    @Override
    public void onNext(T t) {
        onSuccess(t);
    }

    @Override
    public void onError(Throwable e) {
        Log.e("YM","Error:"+e);
       /* NetError error = null;
        if (e != null) {
            //如果不是自定义异常类型
            if (!(e instanceof NetError)) {
                if (e instanceof UnknownHostException) {
                    error = new NetError(e, NetError.NoConnectError);
                } else if (e instanceof JSONException || e instanceof JsonParseException || e instanceof JsonSyntaxException) {
                    error = new NetError(e, NetError.ParseError);
                } else {
                    error = new NetError(e, NetError.OtherError);
                }
            } else {
                error = (NetError) e;
            }
            if (useCommonErrorHandler() && XApi.getCommonProvider() != null) {
                if (XApi.getCommonProvider().handleError(error)) {        //使用通用异常处理
                    return;
                }
            }
            onFail(error);
        }*/
        ApiException exception = null;
        if (e != null) {
            if (e instanceof UnknownHostException) {
                exception = new ApiException(ApiException.UNKNOWN, "未知主机异常");
            } else if (e instanceof JsonParseException || e instanceof JSONException) {
                exception = new ApiException(ApiException.PARSE_ERROR, "解析错误");
            } else if (e instanceof ConnectException) {
                exception = new ApiException(ApiException.NETWORD_ERROR, "连接错误");
            } else if (e instanceof HttpException) {
                HttpException httpException = (HttpException) e;
                exception = new ApiException(httpException.code(), httpException.getMessage());
            } else if (e instanceof SocketTimeoutException) {
                exception = new ApiException(ApiException.TIME_OUT_ERROR, "连接超时");
            } else if (e instanceof IllegalStateException
                    || e instanceof ConnectionShutdownException
                    || e instanceof SSLHandshakeException) {
                exception = new ApiException(ApiException.ILLEGAL_STATE_ERROR, e.toString());
            } else {
                try {
                    exception = (ApiException) e;
                }catch (Exception ee){
                    exception = new ApiException(ApiException.ILLEGAL_STATE_ERROR, "网络错误");
                }
            }
            if (useCommonErrorHandler() && XApi.getCommonProvider() != null) {
                if (XApi.getCommonProvider().handleError(exception)) {        //使用通用异常处理
                    return;
                }
            }
            onFail(exception);
            if (errorAction != null) {
                try {
                    errorAction.accept(exception);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onComplete() {

    }

    protected boolean useCommonErrorHandler() {
        return false;
    }

    public abstract void onSuccess(T t);

    public abstract void onFail(ApiException error);


}
